<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule\Forms;

use Nette\Application\UI\Form;

class MenuItemForm extends Form {
    
    public function __construct($parent, $name)
    {
        parent::__construct($parent, $name);
        
        
        
        $this->addHidden("id");
        $this->addHidden("id_menu");
        $this->addText('title', 'Titulek:')
                ->setRequired('Titulek musí být vyplněn')
                ->setAttribute("class",  "inp-form");
        
        $this->addSelect("id_menu_item", "Nadřazená položka", $this->getParentChoices());
        $this->addSelect("id_page", "Stránka", $this->getPageChoices());
        $this->addText("priority", "Priorita")
            ->setDefaultValue(0);
        $this->addSubmit('send', 'Odeslat');
        $this->onSuccess[] = array($this, 'formSubmited');
    }
    
    protected function getPageChoices() {
        $result = array("0" => "Žádná");
        
        $items = $this->getPresenter()->repositories->article->findAll();
        
        foreach ($items as $item) {
            $result["Články"][$item->getId()] = $item->getTitle();
        }
        
        $items = $this->getPresenter()->repositories->gallery->findAll();
        
        foreach ($items as $item) {
            $result["Fotogalerie"][$item->getId()] = $item->getTitle();
        }
        
        $items = $this->getPresenter()->repositories->news->findAll();
        
        foreach ($items as $item) {
            $result["Novinky"][$item->getId()] = $item->getTitle();
        }
        
        return $result;
    }
    
    protected function getParentChoices() {
        $items = $this->getPresenter()->repositories->menuItem->findAll();
        
        $result = array("0" => "Žádná");
        
        foreach ($items as $item) {
            $result[$item->getId()] = $item->getTitle();
        }
        
        return $result;
    }
    
    public function formSubmited($form)
    {
        if ($form->isValid()) {

            $values = $form->getValues();

            if (!$values->id){

                $menu = $this->getPresenter()->entities->menuItem;
                
                $menu->setId_menu($values->id_menu);
                $menu->setTitle($values->title);
                $menu->setPriority($values->priority);
                
                if ($values->id_menu_item == 0) {
                    $menu->setId_menu_item(Null);
                } else {
                    $menu->setId_menu_item($values->id_menu_item);
                }
                
                if ($values->id_page == 0) {
                    $menu->setId_page(Null);
                } else {
                    $menu->setId_page($values->id_page);
                }
                
                $this->getPresenter()->repositories->menuItem->save($menu);
                
                $this->getPresenter()->flashMessage('Položka menu byla vytvořena.', 'success');
                
                $this->getPresenter()->redirect(':Admin:MenuItem:default', array("menu_id" => $values->id_menu));
            } else {
                
                $menu = $this->getPresenter()->repositories->menuItem->find($values->id);
                
                $menu->setId_menu($values->id_menu);
                $menu->setTitle($values->title);
                $menu->setPriority($values->priority);
                
                if ($values->id_menu_item == 0) {
                    $menu->setId_menu_item(Null);
                } else {
                    $menu->setId_menu_item($values->id_menu_item);
                }
                
                if ($values->id_page == 0) {
                    $menu->setId_page(Null);
                } else {
                    $menu->setId_page($values->id_page);
                }
                
                $this->getPresenter()->repositories->menuItem->save($menu);
                
                $this->getPresenter()->flashMessage('Položka menu byla uložena.', 'success');
                
                $this->getPresenter()->redirect(':Admin:MenuItem:default', array("menu_id" => $values->id_menu));
            }
            
            
            
        }
    }
}

